package com.hpin.mlxteacher.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.utils.Constant;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Thread(new Runnable() {

            @Override
            public void run() {
                SystemClock.sleep(2000);
                handler.sendEmptyMessage(0);
            }
        }).start();

    }

    @Override
    public void widgetClick(View v) {

    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            // 判断能否自动登录
            SharedPreferences sp = getSharedPreferences(Constant.CONFIG, MODE_PRIVATE);
            String phoneNum = sp.getString("phoneNum", "");
            String psw = sp.getString("psw", "");
            if (!TextUtils.isEmpty(phoneNum) && !TextUtils.isEmpty(psw)) {

                //自动登录
                Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                intent.putExtra("phoneNum", phoneNum);
                intent.putExtra("psw", psw);
                startActivity(intent);
                finish();

            } else {

                //进入登录页面
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();

            }

        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        System.exit(0);
    }
}
